<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/28
 * Time: 17:08
 */

namespace app\portal\controller;



use app\portal\service\PostService;
use cmf\controller\HomeBaseController;
use think\Db;

class NewController extends HomeBaseController
{
    public function index(){
        $post_type  = $this->request->param();

        unset($post_type['page']);
        if (empty($post_type)){
            $post_type['post_type']=3;
        }
        $list=Db::name("portal_post")->where($post_type)->where('post_status',1)->order('create_time desc')->paginate(5);
        $this->assign('page', $list->render());
        $new=array();
       if($list){
           foreach ($list as $k=>$v){
               $icon=json_decode($v['more'],true);
               if(strpos($icon['thumbnail'],'http') === false) {
                   $new[$k]['thumbnail']="/public/upload/".$icon['thumbnail'];;
               }else{
                   $new[$k]['thumbnail']=$icon['thumbnail'];
               }
               $new[$k]['post_excerpt']=$v['post_excerpt'];
               $new[$k]['post_title']=$v['post_title'];
               $new[$k]['create_time']=$v['create_time'];
               $new[$k]['id']=$v['id'];
               $new[$k]['post_type']=$v['post_type'];
           }
       }

        $this->assign("new",$new);
       return $this->fetch(":new");
    }
    public function article(){
        $postService = new PostService();
        $pageId      = $this->request->param('id', 0, 'intval');

        $cid     =$this->request->param('cid', 0, 'intval');
        $page        = $postService->publishedPage($pageId,$cid);

        if (empty($page)) {
            abort(404, ' 页面不存在!');
        }

        $where = [
            'post_type'      => $cid,
            'published_time' => [['< time', time()], ['> time', 0]],
            'post_status'    => 1,
            'delete_time'    => 0,
            'id '            => ['<', $pageId]
        ];
        $pre= Db::name("portal_post")->where($where)->order('id', 'DESC')->find();
        if (empty($pre)){
            $pre['post_title']="没有了";
            $pre['url']="javascript:viod()";
        }else{
            $pre['url']="http://".$_SERVER['SERVER_NAME']."/public/portal/new/article/id/".$pre['id']."/cid/".$cid;
        }
        $this->assign('pre', $pre);
        $where = [
            'post_type'      => $cid,
            'published_time' => [['< time', time()], ['> time', 0]],
            'post_status'    => 1,
            'delete_time'    => 0,
            'id '            => ['>', $pageId]
        ];
        $next=Db::name("portal_post")->where($where)->order('id', 'DESC')->find();

        if (empty($next)){
            $next['post_title']="没有了";
            $next['url']="javascript:viod()";
        }else{
            $next['url']="http://".$_SERVER['SERVER_NAME']."/public/portal/new/article/id/".$next['id']."/cid/".$cid;
        }
        $this->assign('next', $next);
       // print_r($next);exit;
        $this->assign('page', $page);

        return $this->fetch(":newarticle");
    }
}